<?php
class InsertPayaccCommand extends CConsoleCommand
{
	public function run($args)
	{
		$users=User::model()->with('service')->findAll();
		foreach ($users as $user) {
			$pay = new Payacc();
			$pay->user_id = $user->id;
			$pay->work_date = '01.01.2010';				#lang=en
			$pay->comment = 'Выравнивание остатков';

			$pay->service_id = $user->service[0]->id;
			
			$getBalance = $user->getBalanceEnd(strtotime("-1day",strtotime(date('Y-m-1'))),true);
			$pay->cost = $getBalance - $user->balance;
			if ($pay->cost > 0) {
				if (!$pay->save()) print_r($pay->getErrors());
				echo 'Dogovor: id='.$user->id.', getBalanceEnd - blanace = '.$getBalance.' - '.$user->balance."\n";
			} else {
				echo '!!!Error. Dogovor: id='.$user->id.', getBalanceEnd - blanace = '.$getBalance.' - '.$user->balance."\n";
			}
		}
	}
}